//
//  SLChatImageCell.m
//  MyChatDemo
//
//  Created by DragonYao on 2017/9/22.
//  Copyright © 2017年 DragonYao. All rights reserved.
//

#import "SLChatImageCell.h"
#import "UIImageView+WebCache.h"

@interface SLChatImageCell ()

@property (nonatomic, strong) UIButton *headImgBtn;//头像
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UIImageView *bgImgView;//背景气泡
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;//菊花

@end

@implementation SLChatImageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //头像
        self.headImgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.headImgBtn addTarget:self action:@selector(showHeaderView:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.headImgBtn];
         //气泡图片
        self.bgImgView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.userInteractionEnabled = YES;
        [self.contentView addSubview:self.bgImgView];
    
        //发送的图片信息
        self.imgView  = [UIImageView new];
        self.imgView.backgroundColor = [UIColor lightGrayColor];
        self.imgView.layer.cornerRadius = 5.0;
        self.imgView.layer.masksToBounds = YES;
        self.imgView.userInteractionEnabled = YES;
        [self.contentView addSubview:self.imgView];
       
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImageView:)];
        [self.imgView addGestureRecognizer:tapGesture];
        //菊花
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)];
        self.indicatorView.hidesWhenStopped = YES;
        [self.imgView addSubview:self.indicatorView];
    }
    return self;
}

- (void)configCellWithImgPath:(NSString *)imgPath isFriends:(BOOL)isFriends {
    //进来之后加载一个默认的frame
    [self loadDefaultImageFrame:isFriends];
    NSURL *url = [NSURL URLWithString:imgPath];
    [self.indicatorView startAnimating];//开启菊花
    [self.imgView sd_setImageWithURL:url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        [self.indicatorView stopAnimating];//停止菊花
        [self.delegate cellImgDidDownloadFinished:self atRow:self.indexRow image:image isError:!error];
        CGFloat imgRadio = image.size.height / image.size.width;
        if (isFriends) {
            if (imgRadio > 1) {//说明是长图
                CGFloat imgH = imgRadio * kMaxImgWidth;
                self.bgImgView.frame = CGRectMake(kHeadW + kPadding * 2, kPadding, kMaxImgWidth + kPadding * 2 + kTriWidth, imgH + kPadding * 2);
                self.imgView.frame = CGRectMake(self.bgImgView.left + kPadding + kTriWidth, self.bgImgView.top + kPadding, kMaxImgWidth, imgH);
            }
            else {//宽图
                CGFloat imgW = kMaxImgHeight/imgRadio > kMaxImgWidth ? kMaxImgWidth : kMaxImgHeight/imgRadio;
                CGFloat imgH = imgRadio * imgW;
                self.bgImgView.frame = CGRectMake(kHeadW + kPadding * 2, kPadding, imgW + kPadding * 2 + kTriWidth, imgH + kPadding * 2);
                self.imgView.frame = CGRectMake(self.bgImgView.left + kPadding + kTriWidth, self.bgImgView.top + kPadding, imgW, imgH);
            }
            self.bgImgView.image = [[UIImage imageNamed:@"chatfrom_imgbg_normal"] stretchableImageWithLeftCapWidth:25 topCapHeight:25];
        }
        else {
            if (imgRadio > 1) {//说明是长图
                CGFloat imgH = imgRadio * kMaxImgWidth;
                CGFloat xOffset = (kWIDTH - (kHeadW + kPadding * 5 + kMaxImgWidth));
                self.bgImgView.frame = CGRectMake(xOffset, kPadding, kMaxImgWidth + kPadding * 2 + kTriWidth, imgH + kPadding * 2);
                self.imgView.frame = CGRectMake(self.bgImgView.left + kPadding, self.bgImgView.top + kPadding, kMaxImgWidth, imgH);
            }
            else {//宽图(如果宽图特别的宽，就以最大宽度为基准)
                CGFloat imgW = kMaxImgHeight/imgRadio > kMaxImgWidth ? kMaxImgWidth : kMaxImgHeight/imgRadio;
                CGFloat imgH = imgRadio * imgW;
                CGFloat xOffset = (kWIDTH - (kHeadW + kPadding * 5 + imgW));
                self.bgImgView.frame = CGRectMake(xOffset, kPadding, imgW + kPadding * 2 + kTriWidth, imgH + kPadding * 2);
                self.imgView.frame = CGRectMake(self.bgImgView.left + kPadding, self.bgImgView.top + kPadding, imgW, imgH);
            }
            self.bgImgView.image = [[UIImage imageNamed:@"chatto_imgbg_normal"] stretchableImageWithLeftCapWidth:25 topCapHeight:25];
        }
    }];
}

- (void)loadDefaultImageFrame:(BOOL)isFriend {
    if (isFriend) {
        self.headImgBtn.frame = CGRectMake(kPadding, kPadding, kHeadW, kHeadH);
        [self.headImgBtn setImage:[UIImage imageNamed:@"friendAvertor"] forState:(UIControlStateNormal)];
        [self.headImgBtn setImage:[UIImage imageNamed:@"friendAvertor"] forState:(UIControlStateHighlighted)];
        self.bgImgView.frame = CGRectMake(kHeadW + kPadding * 2, kPadding, kMaxImgWidth + kPadding * 2 + kTriWidth, kMaxImgHeight + kPadding * 2);
        self.imgView.frame = CGRectMake(self.bgImgView.left + kPadding + kTriWidth, self.bgImgView.top + kPadding, kMaxImgWidth, kMaxImgHeight);
        self.indicatorView.center = self.imgView.center;
    }
    else {
        self.headImgBtn.frame = CGRectMake((kWIDTH - kPadding - kHeadW), kPadding, kHeadW, kHeadH);
        [self.headImgBtn setImage:[UIImage imageNamed:@"meAvertor"] forState:(UIControlStateNormal)];
        [self.headImgBtn setImage:[UIImage imageNamed:@"meAvertor"] forState:(UIControlStateHighlighted)];
        CGFloat xOffset = (kWIDTH - (kHeadW + kPadding * 2 + kMaxImgWidth));
        self.bgImgView.frame = CGRectMake(xOffset, kPadding, kMaxImgWidth + kPadding * 2 + kTriWidth, kMaxImgHeight + kPadding * 2);
        self.imgView.frame = CGRectMake(self.bgImgView.left + kPadding, self.bgImgView.top + kPadding, kMaxImgWidth, kMaxImgHeight);
        self.indicatorView.center = self.imgView.center;
    }
}

//#warning 这里最好通过Model返回宽高 因为这里就算拿到了连接，无法刷新cell高度
+ (CGFloat)cellHeightWithImageRatio:(CGFloat)ratio {
    if (ratio > 1) {//说明是长图
        return ratio * kMaxImgWidth + kPadding * 4;
    }
    else {//宽图(如果宽图特别的宽，就以最大宽度为基准)
        CGFloat imgW = kMaxImgHeight/ratio > kMaxImgWidth ? kMaxImgWidth : kMaxImgHeight/ratio;
        return ratio * imgW + kPadding * 4;
    }
    return kMaxImgHeight + kPadding * 4;
}

#pragma mark - Helper Methods

- (void)showImageView:(UITapGestureRecognizer *)tap {
    SLog(@"显示图片浏览");
}

- (void)showHeaderView:(id)sender {
    SLog(@"点击头像");
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
